//
//  ChainNewsCell.m
//  Chain
//
//  Created by zhaosh on 2018/4/3.
//  Copyright © 2018年 zhaosh. All rights reserved.
//

#import "ChainNewsCell.h"
#import "UIImageView+WebCache.h"

@interface ChainNewsCell()
@property(strong,nonatomic) UIView *containerV;
@property(strong,nonatomic) UIImageView *imgV;
@property(strong,nonatomic) UIImageView *imgV1;
@property(strong,nonatomic) UIImageView *imgV2;

@property(strong,nonatomic) UILabel *titleL;
@property(strong,nonatomic) UILabel *timeL;
@property(strong,nonatomic) UILabel *sourceL;
@property(strong,nonatomic) UIView *bottomLine;
@property(strong,nonatomic) UIView *diviLine;
@property(strong,nonatomic)UIButton *checkButton;

@end

@implementation ChainNewsCell

#pragma mark converse init method
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"newsCell";
    ChainNewsCell *cell = (ChainNewsCell*)[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil){
        cell = [[ChainNewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        [self initView];
    }
    return self;
}

-(void)initView{
    self.checkButton=[[UIButton alloc] init];
    self.checkButton.backgroundColor=[UIColor clearColor];
    [self.checkButton addTarget:self action:@selector(touchCheckBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.checkButton setBackgroundImage:[UIImage imageNamed:@"collect_noSelect"] forState:UIControlStateNormal];
    [self addSubview:self.checkButton];
    self.checkButton.alpha=0;
    
    self.containerV = [UIView new];
    self.containerV.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.containerV];

    self.titleL = [UILabel new];
    self.titleL.font = [UIFont systemFontOfSize:18];
    self.titleL.textColor = [UIColor colorWithHexString:@"#333333"];
    self.titleL.numberOfLines = 2;
    [self.containerV addSubview:self.titleL];
    
    self.imgV = [UIImageView new];
    self.imgV.contentMode = UIViewContentModeScaleAspectFill;
    self.imgV.clipsToBounds = YES;
    [self.containerV addSubview:self.imgV];
    
    self.imgV1 = [UIImageView new];
    self.imgV1.contentMode = UIViewContentModeScaleAspectFill;
    self.imgV1.clipsToBounds = YES;
    [self.containerV addSubview:self.imgV1];
    
    self.imgV2 = [UIImageView new];
    self.imgV2.contentMode = UIViewContentModeScaleAspectFill;
    self.imgV2.clipsToBounds = YES;
    [self.containerV addSubview:self.imgV2];
    
    self.sourceL = [UILabel new];
    self.sourceL.font = [UIFont systemFontOfSize:11];
    self.sourceL.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.containerV addSubview:self.sourceL];
    
    self.diviLine = [UIView new];
    self.diviLine.backgroundColor = HEXCOLOR(@"ebeaea");
    [self.containerV addSubview:self.diviLine];
    
    self.timeL = [UILabel new];
    self.timeL.font = self.sourceL.font;
    self.timeL.textColor = self.sourceL.textColor;
    self.timeL.textAlignment = NSTextAlignmentLeft;
    [self.containerV addSubview:self.timeL];
    
    self.bottomLine = [UIView new];
    self.bottomLine.backgroundColor = ThreadBackgroundColor;
    [self.containerV addSubview:self.bottomLine];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.checkButton.frame=CGRectMake(12, 60, 18, 18);
    if (self.item.isEdit) {
        self.checkButton.alpha=1;
    }else{
        self.checkButton.alpha=0;
    }
    
    CGFloat ScreenWidth_x = ScreenWidth;
    CGFloat startX = 0;
    if (self.fromHome) {
        ScreenWidth_x = ScreenWidth - 20;
        startX = 10;
    }else{
        ScreenWidth_x = ScreenWidth;
        startX = 0;
    }
    self.containerV.frame = CGRectMake(startX, 0, ScreenWidth_x, self.height);
    
    self.titleL.text = self.item.title;
    self.titleL.frame  = CGRectMake(15, 15, ScreenWidth_x - 30, 0);
    [self.titleL sizeToFit];
    
    CGFloat soureceLTop;
    CGFloat timeRight = ScreenWidth_x - 15 - 100;
    
    self.imgV.hidden = NO;
    self.imgV1.hidden = YES;
    self.imgV2.hidden = YES;
    CGFloat imgW = (ScreenWidth_x - 30 - 6) / 3;
    
    NSString *largeImageUrl=[NSString stringWithFormat:@"%@",self.item.largeImageUrl];

    NSMutableArray *listImgArray= [NSMutableArray array];
    NSString *abstractUrl1=[NSString stringWithFormat:@"%@",self.item.abstractUrl1];
    NSString *abstractUrl2=[NSString stringWithFormat:@"%@",self.item.abstractUrl2];
    NSString *abstractUrl3=[NSString stringWithFormat:@"%@",self.item.abstractUrl3];
    
    if (![abstractUrl1 isBlankString]) {
        [listImgArray addObject:self.item.abstractUrl1];
    }
    if (![abstractUrl2 isBlankString]) {
        [listImgArray addObject:abstractUrl2];
    }
    if (![abstractUrl3 isBlankString]) {
        [listImgArray addObject:abstractUrl3];
    }
    
    if (![largeImageUrl isBlankString]) {//大图模式
        self.imgV.frame = CGRectMake(self.titleL.left, self.titleL.bottom + 7, ScreenWidth_x - 30, (ScreenWidth_x - 30) *9 /16);
        if (![largeImageUrl isBlankString]) {
            NSString *url=[NSString stringWithFormat:@"%@/img/%@",BASE_URL,largeImageUrl];
            [self.imgV sd_setImageWithURL:[NSURL URLWithString:url]];
            soureceLTop = self.imgV.bottom + 7;
        } else {
            //无图模式
            soureceLTop = self.titleL.bottom + 20;
            self.imgV.hidden = YES;
        }
    }else{
        if (listImgArray.count == 0) {//无图模式
            soureceLTop = self.titleL.bottom + 20;
            self.imgV.hidden = YES;
        }else if(listImgArray.count == 3){//多图模式
            self.imgV1.hidden = NO;
            self.imgV2.hidden = NO;
            
            self.imgV.frame = CGRectMake(15, self.titleL.bottom + 7, imgW, imgW *9 / 16);
            self.imgV1.frame = CGRectMake(15 + imgW + 3, self.titleL.bottom + 7, imgW, imgW *9 / 16);
            self.imgV2.frame = CGRectMake(15 + (imgW + 3)*2, self.titleL.bottom + 7, imgW, imgW *9 / 16);
            
             NSString *url1=[NSString stringWithFormat:@"%@/img/%@",BASE_URL,listImgArray[0]];
             NSString *url2=[NSString stringWithFormat:@"%@/img/%@",BASE_URL,listImgArray[1]];
             NSString *url3=[NSString stringWithFormat:@"%@/img/%@",BASE_URL,listImgArray[2]];
            
            [self.imgV sd_setImageWithURL:[NSURL URLWithString:url1]];
            [self.imgV1 sd_setImageWithURL:[NSURL URLWithString:url2]];
            [self.imgV2 sd_setImageWithURL:[NSURL URLWithString:url3]];
            
            soureceLTop = self.imgV.bottom + 7;
        }else{//单图模式 居右
            imgW = 112;
            CGFloat imgH = 75;
            self.titleL.frame = CGRectMake(15, 15, ScreenWidth_x - 15 * 3 - imgW, 0);
            [self.titleL sizeToFit];
            self.imgV.frame = CGRectMake(ScreenWidth_x - imgW - 15, self.titleL.top, imgW, imgH);
            
            if (listImgArray.count>0) {
                NSString *url1=[NSString stringWithFormat:@"%@/img/%@",BASE_URL,listImgArray[0]];
                [self.imgV sd_setImageWithURL:[NSURL URLWithString:url1]];
                soureceLTop = self.imgV.bottom - 12;
                timeRight = self.imgV.left - 15 - 100;
            } else {
                //无图模式
                soureceLTop = self.titleL.bottom + 20;
                self.imgV.hidden = YES;
            }
        }
    }
    
    if ([self.item.source isEqualToString:@""] || !self.item.source) {
        self.sourceL.hidden = YES;
    }else{
        self.sourceL.hidden = NO;
    }
    
    NSString *sourceStr = [NSString stringWithFormat:@"来源：%@",self.item.source];
    self.sourceL.text = sourceStr;
    self.sourceL.frame = CGRectMake(self.titleL.left, soureceLTop, 100, 13);
    
    [self.sourceL sizeToFit];
    if (self.sourceL.width > 110) {
        self.sourceL.width = 110;
    }
    
    self.diviLine.frame = CGRectMake(self.sourceL.right + 6, self.sourceL.top + 2, 0.5, self.sourceL.height - 4);
    self.diviLine.hidden = self.sourceL.hidden;
    
    if (self.sourceL.hidden) {
        self.diviLine.right = self.titleL.left;
    }
    
    NSString *dateStr= [Datetime dateStringWithDateStr:self.item.createTime DateFormat:@"YYYY-MM-dd HH:mm"];
    self.timeL.text = dateStr;
    self.timeL.frame = CGRectMake(self.diviLine.right + 6, self.sourceL.top, 100, self.sourceL.height);
    
    self.bottomLine.frame = CGRectMake(15, self.sourceL.bottom + 15, ScreenWidth_x - 30, 0.5);
    
    self.item.rowHeight = self.bottomLine.bottom;
}

-(void)touchCheckBtn:(UIButton *)btn{
    if ([self.item.isSelected isEqualToString:@"1"]) {
         self.item.isSelected=@"0";
         [self.checkButton setBackgroundImage:[UIImage imageNamed:@"collect_noSelect"] forState:UIControlStateNormal];
    }else{
         self.item.isSelected=@"1";
         [self.checkButton setBackgroundImage:[UIImage imageNamed:@"collect_Select"] forState:UIControlStateNormal];
    }
    if ([self.delegate respondsToSelector:@selector(touchCheckBtn:)]) {
        [self.delegate touchCheckBtn:self.item];
    }
}
@end
